package com.starblink.android.basic.widget.recycler

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.text.TextPaint
import android.util.TypedValue
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.dp
import com.starblink.android.basic.util.FontUtil
import com.starblink.android.basic.widget.refresher.SKRefreshFooter2

/**
 *  Create by yuanl at 2023/10/27
 **/
class BottomLineItemDecor(context: Context, textColor: Int, lineColor: Int): ItemDecoration() {

    private val txtPaint = TextPaint().apply {
        isAntiAlias = true
        color = textColor
        textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12f, context.resources.displayMetrics)
        typeface = FontUtil.getTypeface(FontUtil.DM_SANS_MEDIUM)
    }
    
    private val linePaint = Paint().apply {
        color = lineColor
//        color = Color.BLACK
        strokeWidth = lineH
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    private val bottomLineDrawH = 80.dp

    private val lineW = 20.dp.toFloat()

    private val lineH = 1.dp.toFloat()


    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val adapterCount = parent.adapter?.itemCount?:0
        if (adapterCount > 0 && position == adapterCount - 1) {
            outRect.set(0, 0, 0, bottomLineDrawH)
        } else {
            super.getItemOffsets(outRect, view, parent, state)
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)

        val adapterCount = parent.adapter?.itemCount?:0
        if (adapterCount > 0) {
            val vh = parent.findViewHolderForAdapterPosition(adapterCount - 1)
            vh?.itemView?:return

            val view = vh.itemView

            val save = c.save()

            val text = BOTTOM_TEXT
            val textWidth = txtPaint.measureText(text)
            val textDis = txtPaint.fontMetrics.let {
                (it.bottom - it.top) / 2 - it.bottom
            }
            val lineY = view.bottom + bottomLineDrawH.shr(1) + lineH / 2

            val textBottom = lineY + textDis
            val textStart = (parent.width - textWidth) / 2f

            c.drawText(text, textStart, textBottom, txtPaint)

            c.drawLine(textStart - lineW - 6.dp, lineY, textStart - 6.dp, lineY, linePaint)

            c.drawLine(textStart + textWidth + 6.dp, lineY, textStart + textWidth + 6.dp + lineW, lineY, linePaint)

            c.restoreToCount(save)
        }
    }


    companion object {

        const val BOTTOM_TEXT = "I'm a bottom line"

        fun RecyclerView.noMoreData(textColor: Int ? = null, lineColor: Int ?= null) {
            var has = false
            for (i in 0 until itemDecorationCount) {
                if (getItemDecorationAt(i) is BottomLineItemDecor) {
                    has = true
                    break
                }
            }
            if (!has) {
                addItemDecoration(
                    BottomLineItemDecor(this.context,
                        textColor
                            ?: context.color(com.starblink.basic.style.R.color.gu_text_tertiary),
                        lineColor?: Color.parseColor("#33FFFFFF")
                    )
                )
            }
        }

        fun RecyclerView.refresh() {
            for (i in 0 until itemDecorationCount) {
                val decor = getItemDecorationAt(i)
                if (decor is BottomLineItemDecor) {
                    removeItemDecoration(decor)
                    break
                }
            }
        }

        fun SmartRefreshLayout.noBottomLine() {
            //(refreshFooter as? SKRefreshFooter2)?.noBottomLine = true
            setRefreshFooter(SKRefreshFooter2(context, true))
        }

    }

}